package com.xiangdao.controller;

import com.xiangdao.model.User;
import com.xiangdao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class LoginController {
    @Autowired
    UserService userService;



    @RequestMapping(path = "/login")
    public String  loginSuccess(User user, HttpSession session) {
        if(user.getName()==null){
            return null;
        }
        User userName = userService.getUserByUsername(user.getName());
        if (userName != null) {
            //如果有账号则判断账号密码是否正确
            String password = userService.getPasswordByName(user.getName());
            if (password.equals(user.getPassword())) {
                //添加到session保存起来
                session.setAttribute("loginUser", user);
                return "redirect:/home_page/0";
            } else {
                session.setAttribute("fail","密码输入有误！登入失败！");
                return "redirect:/login";
            }
        } else {
            session.setAttribute("fail","账号输入有误！请重新输入！");
            return null;
        }

    }
}
